from flask_learn.ORM增删改查 import models
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine, text

engine = create_engine("mysql+pymysql://root:qaz3357375@127.0.0.1:3306/test?charset=utf8", max_overflow=5)
Session = sessionmaker(bind=engine)
session = Session()

# 查询
# select * from classes;
# r1 = session.query(models.Classes).first()
r1 = session.query(models.Classes).all()
# for item in r1:
#     print(item.id, item.name)

# select id, name from classes;
r2 = session.query(models.Classes.id, models.Classes.name).all()
# print(r2)
# for item in r2:
#     print(item[0], item[1])
#     print(item.id, item.name)

# 给name起个别名
r3 = session.query(models.Classes.id, models.Classes.name.label('xx')).all()
# for item in r3:
#     print(item.id, item.xx)

r4 = session.query(models.Classes).filter(models.Classes.id == 1).all()
# for item in r4:
#     print(item.id, item.name)

r5 = session.query(models.Classes).filter_by(id=1).all()
# for item in r5:
#     print(item.id, item.name)


# 子查询
r6 = session.query(models.Classes).from_statement(text('select * from classes where name=:name')).params(name='ed')
print(r6)


session.close()

'https://www.cnblogs.com/wupeiqi/articles/8259356.html'
